﻿#define _CRT_SECURE_NO_WARNINGS 1
#include <algorithm>
#include <ios>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
class Solution {
public:
    int dfs(vector<vector<int>>& arr, int x, int y, vector<vector<int>>& vis, int len)
    {
        vis[x][y] = 1;
        int n = arr.size(), m = arr[0].size();
        int maxlen = len + 1;
        for (int i = 0; i < 4; i++)
        {
            int xi = dx[i] + x;
            int yi = dy[i] + y;
            if (xi >= 0 && xi < n && yi >= 0 && yi<m && arr[xi][yi]>arr[x][y])
            {
                if (vis[xi][yi] == 0) maxlen = max(maxlen, dfs(arr, xi, yi, vis, len + 1));
                else maxlen = max(maxlen, len + vis[xi][yi] + 1);
            }
        }
        vis[x][y] = maxlen - len;
        return maxlen;
    }

    int solve(vector<vector<int> >& matrix) {
        //深搜
        vector<vector<int>> vis(matrix.size(), vector<int>(matrix[0].size(), 0));
        int ret = 0;
        int n = matrix.size(), m = matrix[0].size();
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (vis[i][j] != 0) ret = max(vis[i][j], ret);
                else ret = max(ret, dfs(matrix, i, j, vis, 0));
            }
        }
        return ret;
    }
};

int main() {
    vector<vector<int>> arr{ {1,2,3},{4,5,6},{7,8,9} };
    int ret = Solution().solve(arr);
    cout << ret;
    return 0;
}
// 64 位输出请用 printf("%lld")
//1 1 1
//1 0 0
//1 0 0
//1 0 0